package com.jingan.nursinghome.regulatory.separate

import android.app.Activity
import android.text.NoCopySpan
import android.util.Log
import cn.android.support.v7.lib.sin.crown.base.BaseListenner
import cn.android.support.v7.lib.sin.crown.kotlin.common.Toast
import cn.android.support.v7.lib.sin.crown.kotlin.common.Toast.toast
import cn.android.support.v7.lib.sin.crown.kotlin.https.Https
import cn.android.support.v7.lib.sin.crown.kotlin.utils.JSonUtils
import cn.android.support.v7.lib.sin.crown.utils.CacheUtils
import cn.android.support.v7.lib.sin.crown.utils.HttpUtils
import cn.android.support.v7.lib.sin.crown.utils.JSONObjectUtils
import cn.android.support.v7.lib.sin.crown.utils.TimeUtils
import com.jingan.nursinghome.regulatory.data.MicrometerTable
import com.jingan.nursinghome.regulatory.model.BaseTimuModel
import com.jingan.nursinghome.regulatory.model.Topicinfo
import com.jingan.nursinghome.regulatory.separate.bean.CreateMic
import com.jingan.nursinghome.regulatory.separate.bean.Mession
import com.jingan.nursinghome.regulatory.separate.bean.SelectXj
import com.jingan.nursinghome.regulatory.separate.bean.tmfl
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import java.io.File
import java.util.ArrayList

/**
 * Created by 彭治铭 on 2018/6/28.
 */
object Uri {
    var base = "http://shptweilao.org.cn:29080/putuoapp/"
    var fbtj = base + "qwadmin/index.php/Home/Fk/fbtj" //发布巡检
    var Fkpingfen = base + "qwadmin/index.php/Home/Fkpingfen"//查看巡检机构，主要沟通人
    var selectfbtj = base + "qwadmin/index.php/Home/Fk/selectfkxjlist"//查看分开巡检
    var fktmfl = base + "qwadmin/index.php/Home/Fk/fktmfl"//查看巡检列表信息
    var qfb = base + "qwadmin/index.php/Home/Fktm/topicinfo"//千分表
    var history = base + "qwadmin/index.php/Home/FktmQfbRectificationsubject"//历史整改
    var submit = base + "qwadmin/index.php/Home/Fktmsubmit"//分开巡检提交
    var submit2 = base + "qwadmin/index.php/Home/Fkpingfen/pfsubmit"//分开巡检提交,图片，签名
    var upload = base + "qwadmin/index.php/Home/Fkupload"//上传
    var delete = base + "qwadmin/index.php/Home/Fkdelete"//删除


    //是否有缓存
    fun haCache(pid: String): Boolean {
        var c = CacheUtils.getInstance().getAsString(pid + "b" + MicrometerTable.getInstance().user.id)
        if (c != null) {
            return true
        }
        return false
    }

    fun clearBitmap(list:MutableList<Topicinfo>){
        list.forEach {
            it.bitmaps?.clear()
        }
    }

    //缓存题目
    fun putCache(pid: String) {
        var key = "千分表" + MicrometerTable.getInstance().user.id + pid
        clearBitmap(topicinfoList)
        CacheUtils.getInstance().put(key, topicinfoList)
        key = "历史整改" + MicrometerTable.getInstance().user.id + pid
        clearBitmap(topicsHostoryQF)
        CacheUtils.getInstance().put(key, topicsHostoryQF)
        if (topicinfoList.size > 0) {
            CacheUtils.getInstance().put(pid + "b" + MicrometerTable.getInstance().user.id, true)
        }
    }

    //获取缓存千分表
    fun getCacheTopicinfo(pid: String): MutableList<Topicinfo>? {
        var key = "千分表" + MicrometerTable.getInstance().user.id + pid
        return CacheUtils.getInstance().getAsList<Topicinfo>(key)
    }

    //获取缓存千分表历史整改
    fun getCacheHostoryTopicinfo(pid: String): MutableList<Topicinfo>? {
        var key = "历史整改" + MicrometerTable.getInstance().user.id + pid
        return CacheUtils.getInstance().getAsList<Topicinfo>(key)
    }

    //新千分表数据
    public var topicinfoList: MutableList<Topicinfo> = ArrayList<Topicinfo>()
        set(value) {
            field.clear()
            field.addAll(value)
        }

    //千分表历史整改数据
    var topicsHostoryQF: MutableList<Topicinfo> = ArrayList<Topicinfo>()
        set(value) {
            field.clear()
            field.addAll(value)
        }

    //发布巡检
    fun fbtj(activity: Activity, stname: String, oid: String, routing_inspection_time: String?, lid: String?, otherpeople: String?, nameid: String?, callbak: BaseListenner<Mession>) {
        var clock = TimeUtils.getInstance().getAbsoluteTime(" HH:mm")
        //Log.e("ui","时间:\t"+(routing_inspection_time+clock ))
        Https(fbtj, activity).addParam("stname", stname).addParam("oid", oid).addParam("routing_inspection_time", routing_inspection_time + clock).addParam("lid", lid).addParam("otherpeople", otherpeople).addParam("nameid", nameid)
                .onSuccess {
                    Log.e("ui", "发布：\t" + it)
                }.onFailure {
                    Toast.show("连接访问异常")
                    Log.e("ui", "发布失败：\t" + it)
                }.showLoad(true).Post(Mession()) {
                    callbak.onResult(it)
                }
    }

    //查看巡检机构，主要沟通人
    fun Fkpingfen(activity: Activity, pid: String, base: BaseListenner<CreateMic>) {
        Https(Fkpingfen, activity).addParam("pid", pid).showLoad(true).onSuccess {
            Log.e("ui", "查看评分结果:\t" + it)
        }.Post(Mession()) {
            if (it.code.equals("0")) {
                var create = JSonUtils.parseObject(it.list, CreateMic())
                base.onResult(create)
            }
        }
    }

    //查看巡检
    fun selectfbtj(activity: Activity, callbak: (list: ArrayList<SelectXj>) -> Unit) {
        Https(selectfbtj, activity).showParams().showLoad(true).addParam("nameid", MicrometerTable.getInstance().user.id).onFailure {
            Toast.show("连接访问异常")
        }.onSuccess {
            Log.e("ui", "查看巡检信息:\t" + it)
        }.Post(Mession()) {
            if (it.code.equals("0")) {
                //成功
                var list = JSonUtils.parseArray(it.list, ArrayList<SelectXj>())
                list?.let {
                    callbak(list)
                }
            } else {
                it.errmsg?.let {
                    Toast.show(it)
                }
            }
        }
    }

    //分开用户巡检_题目分类
    fun fktmfl(activity: Activity, xjid: String, callbak: (list: ArrayList<tmfl>) -> Unit) {
        Log.e("ui", "xjid：\t" + xjid)
        Https(fktmfl, activity).showLoad(true).addParam("xjid", xjid).onSuccess {
            Log.e("ui", "查看巡检信息题目分类:\t" + it)
        }.Post(Mession()) {
            //Log.e("ui", "查看巡检信息:\t" + it)
            if (it.code.equals("0")) {
                //成功
                var list = JSonUtils.parseArray(it.list, ArrayList<tmfl>())
                list?.let {
                    callbak(list)
                }
                Log.e("ui", "成功，题目类别个数:\t" + list?.size)
            } else {
                it.errmsg?.let {
                    Toast.show(it)
                }
            }
        }
    }

    //千分表表数据
    //参数 套题名称，敬老院id
    fun qfb(activity: Activity, pid: String, tmflname: String, success: () -> Unit, fail: (reson: String?) -> Unit) {
        Log.e("ui", "tmflname:\t" + tmflname + "\tpid:\t" + pid)
        Https(qfb, activity).showLoad(true).addParam("pid", pid).addParam("tmflname", tmflname).onSuccess {
            Log.e("ui", "千分表题目:\t" + it)
        }.onFailure {
            Log.e("ui", "千分表失败:\t" + it)
        }.Post(Mession()) {
            if (it.code.equals("0")) {
                //成功
                var list = JSonUtils.parseArray(it.list, ArrayList<BaseTimuModel>())
                list?.let {
                    val topicinfos = ArrayList<Topicinfo>()
                    for (i in 0..list.size - 1) {
                        val data = list.get(i).data
                        val subjectcategory_name = list.get(i).subjectcategory_name
                        if (data != null && data.trim { it <= ' ' } != "null" && data.trim { it <= ' ' } != "") {
                            //var topicinfos2 = JSonUtils.parseArray(data, ArrayList<Topicinfo>())
                            var jsonArray: JSONArray? = null
                            try {
                                jsonArray = JSONArray(data)
                            } catch (e: Exception) {
                                Log.e("test", "千分表题目JSON数据解析异常[历史整改]:\t" + e.message)
                            }

                            val topicinfos2 = JSONObjectUtils.getInstance().getArrayList(jsonArray, Topicinfo::class.java)
                            topicinfos2?.let {
                                (topicinfos2[0] as Topicinfo).subjectcategory_name = subjectcategory_name
                                topicinfos2.forEach {
                                    topicinfos.add(it as Topicinfo)
                                }
                                //topicinfos.addAll(topicinfos2)
                            }
                        }
                    }
                    topicinfoList.clear()
                    topicinfoList.addAll(topicinfos)
                    success()
                }
            } else {
                //失败
                fail(it.errmsg)
            }
        }
    }

    //历史整改数据
    fun history(activity: Activity, pid: String, tmflname: String, success: () -> Unit, fail: (reson: String?) -> Unit) {
        Log.e("ui", "tmflname:\t" + tmflname + "\tpid:\t" + pid)
        Https(history, activity).showLoad(true).addParam("pid", pid).addParam("tmflname", tmflname).onSuccess {
            Log.e("ui", "千分表题目:\t" + it)
        }.onFailure {
            Log.e("ui", "千分表失败:\t" + it)
        }.Post(Mession()) {
            if (it.code.equals("0")) {
                //成功
                var list = JSonUtils.parseArray(it.list, ArrayList<BaseTimuModel>())
                list?.let {
                    val topicinfos = ArrayList<Topicinfo>()
                    for (i in 0..list.size - 1) {
                        val data = list.get(i).data
                        val subjectcategory_name = list.get(i).subjectcategory_name
                        if (data != null && data.trim { it <= ' ' } != "null" && data.trim { it <= ' ' } != "") {
                            //var topicinfos2 = JSonUtils.parseArray(data, ArrayList<Topicinfo>())
                            var jsonArray: JSONArray? = null
                            try {
                                jsonArray = JSONArray(data)
                            } catch (e: JSONException) {
                                Log.e("test", "千分表题目JSON数据解析异常[历史整改]:\t" + e.message)
                            }

                            val topicinfos2 = JSONObjectUtils.getInstance().getArrayList(jsonArray, Topicinfo::class.java)
                            topicinfos2?.let {
                                (topicinfos2[0] as Topicinfo).subjectcategory_name = subjectcategory_name
                                topicinfos2.forEach {
                                    topicinfos.add(it as Topicinfo)
                                }
                                //topicinfos.addAll(topicinfos2)
                            }
                        }
                    }
                    topicsHostoryQF.clear()
                    topicsHostoryQF.addAll(topicinfos)
                    success()
                }
            } else {
                //失败
                fail(it.errmsg)
            }
        }
    }

    //千分表巡检信息提交
    fun submit(map: MutableMap<String, String>, file: MutableMap<String, File>, callbak: (str: String?) -> Unit) {
        Https(submit).addParam(map).addFile(file).onSuccess {
            Log.e("ui", "提交成功:\t" + it)
        }.onFailure {
            callbak(it)
        }.Post(Mession()) {
            if (it.code.equals("0")) {
                callbak(it.list)
            } else {
                callbak(it.errmsg)
            }
        }
    }

    //千分表巡检信息提交，图片，签名
    fun submitTup(activity: Activity, map: MutableMap<String, String>, file: MutableMap<String, File>, callbak: (str: String?) -> Unit) {
        Https(submit2, activity).showLoad(true).addParam(map).addFile(file).onSuccess {
            Log.e("ui", "提交成功:\t" + it)
        }.onFailure {
            callbak(it)
        }.Post(Mession()) {
            if (it.code.equals("0")) {
                callbak(it.list)
            } else {
                callbak(it.errmsg)
            }
        }
    }

    //上传提交
    fun upload(activity: Activity, pid: String, callbak: (str: String?) -> Unit) {
        Https(upload, activity).showLoad(true).addParam("pid", pid).addParam("uid", MicrometerTable.getInstance().user.id).Post(Mession()) {
            if (it.code.equals("0")) {
                callbak(it.list)
            } else {
                callbak(it.errmsg)
            }
        }
    }

    //删除
    fun delete(activity: Activity, xj_id: String, callbak: (str: String?) -> Unit) {
        Https(delete, activity).showLoad(true).addParam("xj_id", xj_id).addParam("uid", MicrometerTable.getInstance().user.id).onFailure {
            Log.e("ui", "删除失败:\t" + it)
            Toast.show("连接访问异常")
        }.onSuccess {
            Log.e("ui", "删除成功:\t" + it)
        }.Post(Mession()) {
            if (it.code.equals("0")) {
                callbak(it.list)
            } else {
                callbak(it.errmsg)
            }
        }
    }

}